/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.desktop.WorkbenchListener;
import org.ops4j.gaderian.Registry;
import org.ops4j.gaderian.impl.RegistryBuilder;

/**
 * Main class, starts a Desktop with the KeyStore plugin. Mainly used for
 * testing.
 * 
 * @author Johan Maasing
 */
public final class Portunis implements WorkbenchListener {

	private Workbench workbench;

	private Portunis() {
		super();
	}

	public static void main(String[] args) {
		Portunis app = new Portunis();
		app.run();
	}

	private void run() {
		// Splashscreen
		JFrame splashFrame = new JFrame("Portunis");
		splashFrame.setUndecorated(true);
		URL splashURL = getClass().getResource(
				"/nu/zoom/swing/desktop/plugin/keystore/resources/splash.png");
		ImageIcon splashImageIcon = new ImageIcon(splashURL);
		splashImageIcon = new ImageIcon(splashURL);
		splashFrame.getContentPane().add(new JLabel(splashImageIcon));
		splashFrame.pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		splashFrame.setLocation(screenSize.width / 2 - splashFrame.getWidth()
				/ 2, screenSize.height / 2 - splashFrame.getHeight() / 2);
		splashFrame.setVisible(true);

		Registry registry = RegistryBuilder.constructDefaultRegistry();
		workbench = (Workbench) registry.getService(
				"nu.zoom.swing.desktop.Workbench", Workbench.class);
		workbench.addWorkBenchListener(this);
		workbench.start();

		splashFrame.dispose();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.WorkbenchListener#close()
	 */
	@Override
	public void close() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nu.zoom.swing.desktop.WorkbenchListener#start()
	 */
	@Override
	public void start() {
		workbench.setTitle("Zoom KeyStore Manager");
		URL url = getClass()
				.getResource(
						"/nu/zoom/swing/desktop/plugin/keystore/resources/keystore.png");
		ImageIcon icon = new ImageIcon(url);
		ArrayList<ImageIcon> icons = new ArrayList<ImageIcon>();
		icons.add(icon);
		workbench.setIcon(icons);
	}

	@Override
	public void visible() {
	}
}
